package com.myspring.service.impl;

import com.myspring.dao.ProductDao;
import com.myspring.po.Product;
import com.myspring.service.ProductService;
import com.myspring.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: ProdectServiceImpl
 * Package: com.mystring.service.impl
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/12/6 21:16
 * @Version 1.0
 */

@Service
public class ProductServiceImpl implements ProductService {
    /*交给Spring注入*/
    @Autowired
    private ProductDao productDao;

    @Autowired
    private TransactionManager transactionManager;


    /*加入事务管理*/
    @Override
    public void updateProduct(Product product) {
//        try {
//            transactionManager.beginTransaction();
//            productDao.updateProduct(product);
//            transactionManager.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//            transactionManager.rollback();
//        } finally {
//            transactionManager.release();
//        }
        productDao.updateProduct(product);
    }


    @Override
    public void saveProduct(Product product) {
//        try {
//            transactionManager.beginTransaction();
//            productDao.saveProduct(product);
//            transactionManager.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//            transactionManager.rollback();
//        } finally {
//            transactionManager.release();
//        }

        productDao.saveProduct(product);
    }

    @Override
    public void deleteProduct(int id) {
//        try {
//            transactionManager.beginTransaction();
//            productDao.deleteProduct(id);
//            transactionManager.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//            transactionManager.rollback();
//        } finally {
//            transactionManager.release();
//        }

        productDao.deleteProduct(id);
    }

    @Override
    public List<Product> findAll() {
        return productDao.findAll();
    }
}
